package calculatePlus;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class KeyPanel extends JPanel implements ActionListener {

    public boolean status;

    private JTextField resultField;
    private JTextField inputField;

    private Expression expression;

    private String tempNum;
    private String result;

    private final static String[] STANDARD = {
            "MC", "MR", "M+", "M-", "MS", "M",
            "%", "√", "x^2", "1/x",
            "CE", "C", "<-", "/",
            "7", "8", "9", "*",
            "4", "5", "6", "-",
            "1", "2", "3", "+",
            "+/-", "0", ".", "="
    };
    private final static String[] SCIENTIFIC = {
            "MC", "MR", "M+", "M-", "MS", "M",
            "x^2", "x^y", "sin", "cos", "tan",
            "√", "10^x", "log", "exp", "mod",
            "↑", "CE", "C", "<-", "/",
            "Π", "7", "8", "9", "*",
            "n!", "4", "5", "6", "-",
            "+/-", "1", "2", "3", "+",
            "(", ")", "0", ".", "="
    };


    public KeyPanel(boolean status) {
        init();
        this.status = status;
        setLayout(new BorderLayout());
        if (!status) {
            standardCalcInit();
        } else {
            scientificCalcInit();
        }
    }

    private void init() {
        expression = new Expression();
        tempNum = "";
        result = "";
    }

    public JTextField getResultField() {
        return resultField;
    }

    public void setResultField(JTextField resultField) {
        this.resultField = resultField;
    }

    public JTextField getInputField() {
        return inputField;
    }

    public void setInputField(JTextField inputField) {
        this.inputField = inputField;
    }


    public void standardCalcInit() {

        JPanel panel = new JPanel(new GridLayout(1, 6));
        JButton jButton;
        for (int i = 0; i < 6; i++) {
            jButton = new JButton(STANDARD[i]);
            panel.add(jButton);
            jButton.addActionListener(this);
        }
        this.add(panel, BorderLayout.NORTH);
        JPanel panel2 = new JPanel(new GridLayout(6, 4));
        for (int i = 6; i < 30; i++) {
            jButton = new JButton(STANDARD[i]);
            panel2.add(jButton);
            jButton.addActionListener(this);
        }
        this.add(panel2, BorderLayout.CENTER);
//        System.out.println("标准");
    }

    public void scientificCalcInit() {

        JPanel panel = new JPanel(new GridLayout(1, 6));
        JButton jButton;
        for (int i = 0; i < 6; i++) {
            jButton = new JButton(SCIENTIFIC[i]);
            panel.add(jButton);
            jButton.addActionListener(this);
        }
        this.add(panel, BorderLayout.NORTH);

        JPanel panel2 = new JPanel(new GridLayout(7, 5));
        for (int i = 6; i < 41; i++) {

            jButton = new JButton(SCIENTIFIC[i]);
            panel2.add(jButton);
            jButton.addActionListener(this);
        }
        this.add(panel2, BorderLayout.CENTER);
//        System.out.println("科学");

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
//        System.out.println("按钮被点击了");
//        System.out.println(this.inputField.getText());
//        this.resultField.setText("hello");

        String click = jButton.getText();


        if (click.equals("1") || click.equals("2") || click.equals("3") || click.equals("4") || click.equals("5")
                || click.equals("6") || click.equals("7") || click.equals("8") || click.equals("9") || click.equals("0")
                || click.equals(".")) {
            this.tempNum += click;
        }

        if (click.equals("+") || click.equals("-") || click.equals("*") || click.equals("/")
                || click.equals("%") || click.equals("x^2") || click.equals("1/x") || click.equals("√")
                || click.equals("sin") || click.equals("cos") || click.equals("tan") || click.equals("mod")
                || click.equals("10^x") || click.equals("log") || click.equals("exp") || click.equals("=")) {
            expression.add(new NumSign(this.inputField.getText(), click));
            this.tempNum = "";
        }
//        "MC", "MR", "M+", "M-", "MS", "M",
        if (click.equals("↑") || click.equals("Π") || click.equals("n!") || click.equals("+/-")
                || click.equals("(") || click.equals(")") || click.equals("MC") || click.equals("MR")
                || click.equals("M+") || click.equals("m-") || click.equals("MS") || click.equals("M")) {
            new Prompt();
        }

        if (click.equals("<-")) {
            this.tempNum = this.tempNum.substring(0, this.tempNum.length() - 1);
        }

        if (click.equals("C") || click.equals("CE")) {
            this.expression.clear();
            this.inputField.setText("");
            this.tempNum = "";
        }

        // 显示输入框内容
        this.inputField.setText(tempNum);

        // 显示结果框内容
        result = expression.toString();
        this.resultField.setText(result);


        // 计算结果
        if (click.equals("=")) {
            expression.calculate();
            this.resultField.setText(result/* + this.expression.get(0).getStrNum()*/);
            this.inputField.setText(this.expression.get(0).getStrNum());
            this.expression.clear();
            this.result = "";
            this.tempNum = "";
        }

    }
}
